const path = require('path')
const inquirer = require('inquirer')
const chalk = require('chalk');
const { delTemplatePkg, editTemplatePkg } = require('../file')
const cmdMap = {
        node: '10.0.0',
        npm: '6.0.0',
    }
    // 模板一路径
function initQuestions(callback) {
    const templates = require('../custom/index.json');
    if (!Array.isArray(templates)) {
        console.log(chalk.bgRed('未配置模板,请先新增模板'))
        return
    } else {
        if (templates.length === 0) {
            console.log(chalk.bgRed('未配置模板,请先新增模板'))
            return
        }
        let pathUrls = {};
        let names = [];
        templates.map(item => {
                pathUrls = {
                    ...pathUrls,
                    [item.name]: {
                        pathUrl: item.path,
                        id: item.id
                    }
                }
                names.push(item.name)
                    // names.push(`模板名称:${chalk.green(item.name)}${item.command?`,快捷指令${chalk.green(item.command)}`:''}`)
            })
            // 问题生成
        const questions = [{
            type: 'list',
            message: '请选择一种页面模板:',
            name: 'template',
            choices: names,
            validate(val) {
                if (val === '') {
                    return '请输入页面模板!'
                }
                return true
            },
        }]
        inquirer.prompt(questions).then((answer) => {
            if (callback) {
                callback(answer, pathUrls)
            }
            return answer
        })
    }

}
// 模板生成或者编辑
function TemplateEdit() {
    // 问题生成
    const questions = [{
        type: 'list',
        message: '生成模板或新增模板',
        name: 'edit',
        default: '生成模板',
        choices: [
            new inquirer.Separator("--- 分隔符 ---"), // 自定义分隔符
            chalk.red('生成模板'),
            chalk.green('新增模板'),
            chalk.bgGreen('修改模板'),
            chalk.bgRed('删除模板'),
            new inquirer.Separator("--- 分隔符 ---"), // 自定义分隔符
        ],
        validate(val) {
            if (val === '') {
                return '请选择!'
            }
            return true
        },
    }]
    return inquirer.prompt(questions).then((answer) => {
        return answer
    })
}
// 模板生成
function createTemplateFunc() {
    // 问题生成
    const questions = [{
            type: 'input',
            message: chalk.bgBlue('请输入模板名'),
            name: 'name',
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入模板路径(全路径)'),
            name: 'path',
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入当前指令的快捷命令(xl-order 快捷命令)'),
            name: 'command',
        }
    ]
    return inquirer.prompt(questions).then((answer) => {
        return answer
    })
}
// 模板删除
function delTemplateFunc() {
    const templates = require('../custom/index.json');
    if (!Array.isArray(templates)) {
        console.log(chalk.bgRed('未配置模板,请先新增模板'))
        return
    } else {
        if (templates.length === 0) {
            console.log(chalk.bgRed('未配置模板,请先新增模板'))
            return
        }
        let lists = [];
        templates.map((item, index) => {
                lists.push({
                    key: `${index + 1}`,
                    name: item.name,
                    // name: `模板名称:${chalk.green(item.name)}${item.command?`,快捷指令${chalk.green(item.command)}`:''}`,
                    value: item.id
                })
            })
            // 问题生成
        const questions = [{
            type: "rawlist",
            message: chalk.red("请选择需要删除模板："),
            name: "del",
            choices: lists
        }]
        inquirer.prompt(questions).then((answer) => {
            delTemplatePkg(answer.del)
        })
    }

}
/**
 * @function 修改模板
 */
function editTemplateFunc() {
    const templates = require('../custom/index.json');
    if (!Array.isArray(templates)) {
        console.log(chalk.bgRed('未配置模板,请先新增模板'))
        return
    } else {
        if (templates.length === 0) {
            console.log(chalk.bgRed('未配置模板,请先新增模板'))
            return
        }
        let lists = [];
        let edits = {};
        templates.map((item, index) => {
                lists.push({
                    key: `${index + 1}`,
                    name: item.name,
                    // name: `模板名称:${chalk.green(item.name)}${item.command?`,快捷指令${chalk.green(item.command)}`:''}`,
                    value: item.id
                })
                edits = {
                    ...edits,
                    [item.id]: {
                        name: item.name,
                        pathUrl: item.path
                    }
                }
            })
            // 问题生成
        const questions = [{
            type: "rawlist",
            message: chalk.red("请选择需要修改的模板："),
            name: "edit",
            choices: lists
        }]

        inquirer.prompt(questions).then((answer) => {
            const { edit } = answer
            // 编辑问题
            const editQuestions = [{
                    type: 'input',
                    message: chalk.bgBlue('请输入修改后的模板名'),
                    name: 'name',
                    default: edits[edit].name
                },
                {
                    type: 'input',
                    message: chalk.bgBlue('请输入修改后的模板路径(全路径)'),
                    name: 'pathUrl',
                    default: edits[edit].pathUrl
                },
                {
                    type: 'input',
                    message: chalk.bgBlue('请输入当前指令的快捷命令(xl-order 快捷命令)'),
                    name: 'command',
                    default: edits[edit].command
                }
            ]
            inquirer.prompt(editQuestions).then((answer2) => {
                const { name, pathUrl, command } = answer2
                editTemplatePkg({
                    edit,
                    name,
                    pathUrl,
                    command
                })
            })
        })
    }
}
/**
 * @function 模板1 生成动态配置项
 */
function template1Questions() {
    // 问题生成
    const questions = [{
            type: 'input',
            message: chalk.bgBlue('请输入页面名称'),
            name: 'pageName',
            default: ''
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入页面model名(可生成后自行修改)'),
            name: 'namespace',
            default: 'organization'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入页面监听路由'),
            name: 'listenRouter',
            default: '/userCentre/organization'
        },
    ]
    return inquirer.prompt(questions).then((answer) => {
        return answer
    })
}
/**
 * @function 模板4 生成动态配置项
 */
function template4Questions() {
    // 问题生成
    const questions = [{
            type: 'input',
            message: chalk.bgBlue('请输入页面model名(可生成后自行修改)'),
            name: 'namespace',
            default: 'goodsPOSManage'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入新增页面监听路由'),
            name: 'listenAddRouter',
            default: '/goods/posManage/handle/add'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入编辑页面监听路由'),
            name: 'listenEditRouter',
            default: '/goods/posManage/handle/edit/:id'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入详情页面监听路由'),
            name: 'listenDetailRouter',
            default: '/goods/posManage/handle/detail/:id'
        },
    ]
    return inquirer.prompt(questions).then((answer) => {
        return answer
    })
}
/**
 * @function 新增,修改,详情表单模板
 * @returns promise
 */
function template6Questions() {
    // 问题生成
    const questions = [{
            type: 'input',
            message: chalk.bgBlue('请输入页面model名(可生成后自行修改)'),
            name: 'namespace',
            default: 'goodsPOSManage'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入新增页面监听路由'),
            name: 'listenAddRouter',
            default: '/goods/posManage/handle/add'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入编辑页面监听路由'),
            name: 'listenEditRouter',
            default: '/goods/posManage/handle/edit/:id'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入详情页面监听路由'),
            name: 'listenDetailRouter',
            default: '/goods/posManage/handle/detail/:id'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入新增页名称'),
            name: 'addName',
            default: '新增'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入编辑页名称'),
            name: 'editName',
            default: '编辑'
        },
        {
            type: 'input',
            message: chalk.bgBlue('请输入详情页名称'),
            name: 'detailName',
            default: '详情'
        },
        // {
        //     type: 'input',
        //     message: chalk.bgBlue('请输入指定返回路由(非必填)'),
        //     name: 'goBackRouter',
        // },
    ]
    return inquirer.prompt(questions).then((answer) => {
        return answer
    })
}
module.exports = {
    cmdMap,
    initQuestions,
    TemplateEdit,
    createTemplateFunc,
    delTemplateFunc,
    editTemplateFunc,
    template1Questions,
    template4Questions,
    template6Questions
}